package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> list=menuService.findAll();
            return new Result(true, "菜单查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单查询失败");
        }
    }
    @RequestMapping("/add")
    public Result add(Integer[] menuIds,@RequestBody Menu menu){
        try {
            menuService.add(menuIds,menu);
            return new Result(true,"新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败,您只能选择一个父菜单");
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,"查询菜单成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }
    @RequestMapping("/findMenuParentId")
    public Result findMenuParentId(Integer id){
        try {
            List<Integer> list=menuService.findMenuParentId(id);
            return new Result(true,"父菜单回显成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"父菜单回显失败");
    }
    @RequestMapping("/edit")
    public Result edit(Integer[] menuIds,@RequestBody Menu menu){
        try {
            menuService.edit(menuIds,menu);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败,您只能选择一个父菜单");
        }
    }
    @RequestMapping("/deleteMenu")
    public Result deleteMenu(Integer id){
        try {
            menuService.deleteMenu(id);
            return new Result(true,"菜单删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单删除失败,您不能删除含有子菜单的父菜单");
        }
    }

    //动态获取用户菜单
    @RequestMapping("/getDynamicMenu")
    public Result getDynamicMenu(String username){
        List<Menu> menuList = menuService.getDynamicMenu(username);//通过service查询到所有的一级菜单(包含二级菜单)
        try {
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam("roleId") Integer id){
        try {
            List<Integer> list=menuService.findMenuIdsByRoleId(id);
            return new Result(true,"查询菜单成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }

    }
}
